package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.camptower.FourSealModule;

import java.util.Map;

/**
 * 阵营塔 - 四象归一养成界面
 *
 * @ClassName CMD_FourSealInfo
 * @Author jingtao-zheng
 * @Date 2022/8/9 14:08
 * @Version 1.0
 **/
@MessageType(proto = Command.FourSealInfo, classtype = CampTowerMessage.FourSealInfoRequest.class)
public class CMD_FourSealInfo extends Command<CampTowerMessage.FourSealInfoRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.FourSealInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CampTowerMessage.FourSealInfoResponse.Builder response = CampTowerMessage.FourSealInfoResponse.newBuilder();
        FourSealModule fourSealModule = FourSealModule.get(playerCode);
        if(fourSealModule == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        Map<Integer, Integer> fourSeal = fourSealModule.getFourSeal();
        for (Map.Entry<Integer, Integer> integerIntegerEntry : fourSeal.entrySet()) {
            CampTowerMessage.FourSeal.Builder fourSealResponse = CampTowerMessage.FourSeal.newBuilder();
            CampTowerMessage.LineupType lineupType = matchLineupType(integerIntegerEntry.getKey());
            fourSealResponse.setType(lineupType);
            fourSealResponse.setLv(integerIntegerEntry.getValue());
            response.addFourSealDatas(fourSealResponse);
        }

        send(messageSource, response, Result.Success);
    }

    private CampTowerMessage.LineupType matchLineupType(int type){
        if(type == Constant.WEI){
            return CampTowerMessage.LineupType.valueOf(Constant.WEI);
        }
        if(type == Constant.SHU){
            return CampTowerMessage.LineupType.valueOf(Constant.SHU);
        }
        if(type == Constant.WU){
            return CampTowerMessage.LineupType.valueOf(Constant.WU);
        }
        if(type == Constant.QUN){
            return CampTowerMessage.LineupType.valueOf(Constant.QUN);
        }
        return null;
    }

    private void send(MessageSource messageSource, CampTowerMessage.FourSealInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
